package com.activitycode.main;


import java.util.ArrayList;
import java.util.List;

import com.activitycode.view.PageIndicatorView;


import com.duostec.zhuyue.pets.R;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;


public class WelcomeFragment extends Fragment implements OnPageChangeListener{
	private ViewPager mViewPager;
	private PageIndicatorView mPageIndicatorView;
	private SingleViewPagerAdapter adapter;
	private ViewPagerAdapter adapter1;
	private List<Integer> imgList = new ArrayList<Integer>();
	private List<View> viewList = new ArrayList<View>();
	private Context context;
	public IntentOnClickListener intentOnClickListener;
	
	public WelcomeFragment(Context ct){
		this.context = ct;
		
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_wel, container, false);
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		mViewPager = (ViewPager) getView().findViewById(R.id.fragment_wel_view);
		mPageIndicatorView = (PageIndicatorView) getView().findViewById(R.id.fragment_wel_pageIndicatorView);
		initData();
	}
	
	private void initData(){
//		imgList.add(R.drawable.welcome1);
//		imgList.add(R.drawable.welcome2);
//		imgList.add(R.drawable.welcome3);
//		imgList.add(R.drawable.welcome4);
		imgList.add(R.drawable.welcome5);
		for(int i=0;i<imgList.size();i++){
			View view = LayoutInflater.from(context).inflate(R.layout.adapter_viewpager_welcome, null);
			ImageView img = (ImageView) view.findViewById(R.id.adapter_wel_imageView1);
			img.setImageDrawable(context.getResources().getDrawable(imgList.get(i)));
			Button imgButton = (Button) view.findViewById(R.id.adapter_wel_imageButton1);
			if(i==(imgList.size()-1)){
				imgButton.setVisibility(View.VISIBLE);
				imgButton.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						doIntentOnClickListener();

					}
					
				});
			}else{
				imgButton.setVisibility(View.GONE);
			}
			
			viewList.add(view);
		}
//		adapter = new SingleViewPagerAdapter(context,imgList);
		adapter1 = new ViewPagerAdapter(viewList);
		mViewPager.setAdapter(adapter1);
		mViewPager.setOnPageChangeListener(this);
		mPageIndicatorView.setTotalPage(imgList.size());
		mPageIndicatorView.setCameraDistance(20);
		mPageIndicatorView.setRedius(6);
		mPageIndicatorView.setSpace(20);
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPageSelected(int arg0) {
		// TODO Auto-generated method stub
		Log.i("Task", "arg="+arg0);
		mPageIndicatorView.setCurrentPage(mViewPager.getCurrentItem());

	}
	
	//
 	public interface IntentOnClickListener {
 		  public void   onClick();
 	}
 	public void doIntentOnClickListener() {
 	    intentOnClickListener.onClick();
 	  }
 	 
    public void setIntentOnClickListener(IntentOnClickListener callback) {
        this.intentOnClickListener = callback;
    }
	
	
}
